home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 445_01 / pi5ways / cannon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-05  |  3.3 KB  |  107 lines

  1. /**************************************************************************/
  2. /*     Calculate π by "shooting a cannon" at a round pond of radius 1     */
  3. /*                'inscribed' in a 2 x 2 square of land.                  */
  4. /*     This is a variation of the "Monte Carlo" method of simulation      */
  5. /*                                                                        */
  6. /*       Note|  Center of circle is at (1,1).  Corners of land are:       */
  7. /*                    (0,0), (2,0), (2,2), (0,2).                         */
  8. /*                                                                        */
  9. /*                               M\Cooper                                */
  10. /*                       3425 Chestnut Ridge Rd.                          */
  11. /*                        Grantsville, MD 21536                           */
  12. /*                       -------------------------                        */
  13. /*                       email: thegrendel@aol.com                        */   
  14. /*                                                                        */
  15. /*                                   06/91                                */
  16. /*                  Source code placed in the public domain               */
  17. /**************************************************************************/
  18.  
  19.  
  20.  
  21.  
  22. #include <math.h>
  23. #include <stdlib.h>
  24. #include <conio.h>
  25. #include <stdio.h>
  26.  
  27. #define MAXSHOTS 50000
  28. #define MAXNUM 20000
  29. #define UPPER_LIMIT 2
  30.  
  31. /**********************************************************************/
  32. /*                           GETRAND()                                */
  33. /*               Outputs random number (0.0 - 2.0)                    */
  34. /**********************************************************************/
  35.  
  36. double getrand()
  37.  
  38. {
  39.    double scale_factor,   /*scaling factor*/
  40.           rndnum,
  41.           rand0_2;    /*random number between 0.0 and 2.0 returned by fn*/
  42.  
  43.       scale_factor = (double)MAXNUM / (double)UPPER_LIMIT;
  44.       rndnum = (double)random( MAXNUM );
  45.  
  46.       rand0_2 = rndnum / scale_factor;
  47.  
  48.       return( rand0_2 );
  49. }
  50.  
  51. /**************************************************************************/
  52.  
  53.  
  54. void main()
  55. {
  56.    double  X,
  57.            Y,
  58.            Dx,
  59.            Dy,
  60.            distance,
  61.            ratio,
  62.            Pi;
  63.    long shots = 0,
  64.         splashes = 0,
  65.         thuds = 0;
  66.  
  67.       randomize();
  68.       clrscr();     
  69.  
  70.  
  71.       while( shots < MAXSHOTS )
  72.          {
  73.  
  74.          shots++;
  75.  
  76.          X =  getrand();   /*Generate X and Y coordinates*/
  77.          Y =  getrand();   /* of shots */
  78.          /* Origin is at (1.0,1.0), remember */
  79.  
  80.          Dx = X - 1.0;   /*find distance from origin*/   
  81.          Dy = Y -  1.0;
  82.          distance = hypot( Dx,Dy );  /* by Pythagorean Theorem */
  83.  
  84.  
  85.          if( distance <= 1.0 )
  86.             {
  87.             splashes++;
  88.             printf("SPLASH!     ");
  89.             }
  90.           else 
  91.             {
  92.             thuds++;
  93.             printf("THUD.       ");
  94.             }
  95.  
  96.           printf("%5ld splashes | %5ld thuds    ", splashes, thuds);
  97.  
  98.           ratio = (double)splashes / (double)shots;  /* Should be ≈ ¼π */
  99.           Pi = ratio * 4.0;
  100.  
  101.           printf("At %5ld shots  ---- π ≈ %2.8f\n", shots, Pi);
  102.  
  103.           }
  104.  
  105.  
  106. }
  107.